<template>
  <div>
    <el-card style="margin: 10px 0">
      <el-table style="width: 100%" border :data="records">
        <el-table-column type="index" label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="skuDesc" label="描述" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template v-slot="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 80px; height: 80px" />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量(kg)" width="80">
        </el-table-column>
        <el-table-column prop="price" label="价格(原)" width="80">
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="300" align="center">
          <template v-slot="{ row, $index }">
            <el-button
              type="success"
              icon="el-icon-bottom"
              size="mini"
              @click="handleDown(row)"
              v-if="row.isSale == 0"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-top"
              size="mini"
              @click="handleUp(row)"
              v-else
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit"
            ></el-button>
            <el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              @click="getSkuInfo(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        align="center"
        :current-page="pageNum"
        :page-sizes="[3, 5, 10]"
        :page-size="limit"
        layout=" prev, pager, next, jumper,->,sizes,total"
        :total="totalCount"
        @size-change="handleSizeChange"
        @current-change="getDate"
      >
      </el-pagination>

      <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        :direction="direction"
        :show-close="false"
        size="50%"
      >
        <el-row :gutter="20">
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{ skuInfo.price }}元</el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5">平台属性</el-col>
          <el-col :span="16">
            <el-tag
              type="success"
              v-for="(skuAttrValue, index) in skuInfo.skuAttrValueList"
              :key="skuAttrValue.id"
              style="margin: 0px 3px"
              >{{ skuAttrValue.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <el-carousel height="300px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" style="width: 100%; height: 300px" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      pageNum: 1,
      limit: 3,
      totalCount: 0,
      records: [],
      skuInfo: {},
      drawer: false,
      direction: "rtl",
    };
  },
  mounted() {
    this.getDate();
  },
  methods: {
    //得到sku列表数据
    async getDate(page = 1) {
      this.pageNum = page;
      let { pageNum, limit } = this;
      let result = await this.$API.sku.reqSkuList(pageNum, limit);
      // console.log(result);
      if (result.code == 200) {
        this.records = result.data.records;
        this.totalCount = result.data.total;
      }
    },
    //改变每页显示的数据
    handleSizeChange(sizeLimit) {
      this.limit = sizeLimit;
      this.getDate();
    },
    // 上架
    async handleUp(row) {
      let result = await this.$API.sku.reqHandleUp(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "上架成功" });
        row.isSale = 0;
      }
    },
    //下架
    async handleDown(row) {
      let result = await this.$API.sku.reqHandleDown(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "下架成功" });
        row.isSale = 1;
      }
    },
    //开发按钮的回调
    edit() {
      this.$message("正在开发中");
    },
    //得到某个sku的详细信息
    async getSkuInfo(row) {
      let result = await this.$API.sku.reqSkuInfo(row.id);
      // console.log(result);
      if (result.code == 200) {
        this.skuInfo = result.data;
        this.drawer = true;
      }
    },
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px;
}
</style>
<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: red;
}
</style>